package cc.toprank.syncdatatask.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
 

/**
 * 功能：实现发送短信 
 * 作者:黄小云 
 * 日期:2017-02-07 
 * 描述：当系统出现异常时，将会发短信到指定的手机号码
 */
@SuppressWarnings("deprecation")
public class SmsHelper {

	private static Logger logger = Logger.getLogger(SmsHelper.class);
	private static SmsHelper instance = new SmsHelper();

	private SmsHelper() {

	}

	public synchronized static SmsHelper getInstance() {
		if (instance == null) {
			instance = new SmsHelper();
		}

		return instance;
	}

	public void sendSms(String content) {
		JSONObject result = new JSONObject();
		String smsSenderId = PropFileHelper.getInstance().getSmsConfig("sms.sender.id");
		String smsSenderUrl = PropFileHelper.getInstance().getSmsConfig("sms.sender.url");
		String smsSenderUserId = PropFileHelper.getInstance().getSmsConfig("sms.sender.userid");
		String smsSenderUserPassword = PropFileHelper.getInstance().getSmsConfig("sms.sender.password");
		String smsTargetPhone = PropFileHelper.getInstance().getSmsConfig("sms.target.phone");
		try {
			JSONObject sendJson = new JSONObject(); 

			sendJson.put("id", smsSenderId);
			sendJson.put("method", "send");

			JSONObject jsonParams = new JSONObject();
			jsonParams.put("userid", smsSenderUserId);
			jsonParams.put("password", smsSenderUserPassword);

			JSONObject jsonSubmits = new JSONObject();
			jsonSubmits.put("content", content + "【不用等科技】"); 
			jsonSubmits.put("phone", smsTargetPhone); 

			JSONArray jsonArray = new JSONArray();
			jsonArray.add(jsonSubmits);

			jsonParams.put("submit", jsonArray);
			sendJson.put("params", jsonParams); 
   
			 String resultStr = "";//submitUrl(sendJson, smsSenderUrl);
 
			 result = JSON.parseObject(resultStr);
			// 处理returnCode
			this.parseErrorCode(result);  
		} catch (Exception e) {
			result.put("error", "fail");
			result.put("msg", e.getMessage());
			logger.error("发送短信异常" + e.getMessage());
			MailHelper.getInstance().sendMail("发送短信异常", "原因可能是:" + e.getMessage());
		} 
	} 

	 
	public  void parseErrorCode(JSONObject result) {
		JSONArray resultArr = (JSONArray) result.get("result");
		JSONObject resultArrOne = (JSONObject) resultArr.get(0);
		String errorCode = resultArrOne.getString("return");
		if (!errorCode.equals("0")) {
			String errorInfo = this.parseErrorCode(errorCode);
			result.put("returnMsg", errorInfo);
			logger.debug("短信发送结果:" + errorInfo);
		} 
	}
	
	public String parseErrorCode(String errorCode) {
		String errorInfo = "";
		switch (errorCode) {
		case "1001001":
			errorInfo = "参数不完整";
			break;
		case "1001002":
			errorInfo = "余额不足";
			break;
		case "1001003":
			errorInfo = "账号已冻结";
			break;
		case "1001004":
			errorInfo = "密码不正确";
			break;
		case "1001005":
			errorInfo = "访问IP受限";
			break;
		case "1001006":
			errorInfo = "并发数超限";
			break;
		case "1001007":
			errorInfo = "短信附加语不符";
			break;
		case "1004001":
			errorInfo = "手机号不可识别";
			break;
		case "1004002":
			errorInfo = "未配置有效发送规则";
			break;
		case "1004003":
			errorInfo = "短信内容必须有签名";
			break;
		case "1004004":
			errorInfo = "短信内容必须指定签名";
			break;
		case "1004005":
			errorInfo = "短信内容指定签名";
			break;
		case "1004006":
			errorInfo = "无可用通道";
			break;
		case "1004007":
			errorInfo = "模板不匹配";
			break;
		case "1004008":
			errorInfo = "不能发黑名单号码";
			break;
		case "1004009":
			errorInfo = "非白名单号码";
			break;
		case "1004010":
			errorInfo = "不能发黑词内容";
			break;
		case "1004011":
			errorInfo = "内容中不包括白词";
			break;
		case "1004012":
			errorInfo = "定时发送时间参数不正确";
			break;
		case "1004013":
			errorInfo = "定时发送时间不能大于50天";
			break;
		case "1004014":
			errorInfo = "定时发送时间不能小于当前时间";
			break;
		case "1004015":
			errorInfo = "短信签名限20字符";
			break;
		case "1001999":
			errorInfo = "接口系统错";
			break;
		default:
			errorInfo = "其它错误！";
			break;
		}
		return errorInfo;
	}
	
/*	public  String submitUrl(JSONObject jsonobject, String url) throws IOException, HttpException {
		HttpClient httpClient = new HttpClient();
		PostMethod method = new PostMethod(url);
		method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
		httpClient.getHttpConnectionManager().getParams().setSoTimeout(300000);

		RequestEntity requestEntity = new StringRequestEntity(jsonobject.toString());
		method.setRequestEntity(requestEntity);
		httpClient.executeMethod(method);
		String result = method.getResponseBodyAsString();
		return result;
	}*/
	
	public static void main(String[] args)  {

		SmsHelper.getInstance().sendSms("海上生明月，天涯共此时。"); 
	}
}
